<template>
  <div>
      <el-dialog :title="obj.title" :visible.sync="obj.isShow" width="300px">
          <el-upload
                class="upload-demo"
                :action="action"
                ref="upload"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :on-success="hanleSuccess"
                :on-progress="hanleProgress"
                :on-change="close"
                :multiple="false"
                :data="obj"
                :limit="1"
                name="file"
                :on-exceed="handleExceed"
                :file-list="fileList"
                :auto-upload="false"
            >
                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                <el-button style="margin-left: 10px;" size="small" type="success" :disabled="loading?true:false" :icon="loading?'el-icon-loading':''" @click="submitUpload">上传到服务器</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
     </el-dialog>
  </div>
</template>
<script>
    import api from '../api'
    import config from '../config.js'
    export default{
        mounted(){
            
        },
        props:['obj','getBook','alert'],
        methods:{
            handleRemove(file, fileList) {
               this.file=false;
            },
            handlePreview(file) {
                console.log(file);
            },
            close(file){
                this.file=file;
            },
            submitUpload() {
                if(this.file){
                    this.loading=true;
                    this.$refs.upload.submit();
                }
            },
            handleExceed(files, fileList) {
                this.alert(`当前限制选择 1 个文件`);
            },
            hanleSuccess(response, file, fileList){
                console.log(response);
                if(response.code==1){
                    this.$message({
                        message: response.msg,
                        type: 'success'
                    });
                    this.fileList=[];
                    this.getBook();
                    this.loading=false;
                    this.obj.isShow=false;
                }
            },
            hanleProgress(){
                this.loading=true;
            }
        },
        data(){
            console.log(this.obj);
            return {
                action:config.base+"/system/BookUpdateImg",
                fileList: [],
                file:false,
                loading:false
            }
        }
    }
</script>
<style>
    .el-dialog__body{
        padding:20px;
    }
    .el-form-item__error{
        padding-top:0;
    }
</style>
